import re
import sys
from PyQt5 import QtWidgets
from PyQt5.QtCore import pyqtSignal, QThread
from PyQt5.QtWidgets import *
from PyQt5.uic import loadUi

import sys
from PyQt5 import QtWidgets
from PyQt5.QtWidgets import *
from connectUI import Ui_MainWindow
from func_main import MainUI

picDict = {}


class ConnectUI(QtWidgets.QMainWindow, Ui_MainWindow):
    def __init__(self):
        super(ConnectUI, self).__init__()
        loadUi('connectUI.ui', self)
        self.cancelButton.clicked.connect(self.close)
        self.connectButton.clicked.connect(self.showMainWindow)

    def showMainWindow(self):
        comp = re.compile(r'^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$')
        if comp.match(self.lineEdit.text()):
            self.newWindow = MainUI(picDict, self.lineEdit.text())
            self.newWindow.setFixedSize(640, 480)
            self.newWindow.show()
            self.close()


if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    f = open('pics.txt', 'r')
    content = f.read()
    lines = content.split('\n')
    i = 0
    for line in lines:
        items = line.split(' ')
        listFilename = items[0]
        listName = items[1]
        signedIn = False
        accuracy = 0.0
        picDict[i] = [listFilename, listName, signedIn, accuracy]
        i = i + 1
    f.close()
    MainWindow = ConnectUI()
    MainWindow.setWindowTitle('Board IP setup')
    MainWindow.setFixedSize(640, 160)
    MainWindow.show()
    sys.exit(app.exec_())
